package job.manager.client.mb;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import job.manager.db.sb.JobRemote;
import job.manager.db.sb.ProfileRemote;
import job.manager.db.sb.TimeRemote;
import job.manager.db.sb.UserRemote;

/**
 *  This is the bug fix class that helps retreving refrences to EJB beans.
 *  Calling these methods allways returns a refrence.
 */
public class ConnectFactory {

    private static ProfileRemote profile;

    private static UserRemote user;

    private static TimeRemote time;

    private static JobRemote job;

    public static ProfileRemote getProfileBean() {
        if( profile == null){
        try {
            Context c = new InitialContext();
            profile = (ProfileRemote) c.lookup("java:comp/env/ProfileBean");
        } catch (NamingException ne) {
            throw new RuntimeException(ne);
        }
        }
        return profile;
    }

    public static UserRemote getUserBean() {
        if (user == null) {
            try {
                Context c = new InitialContext();
                user = (UserRemote) c.lookup("java:comp/env/UserBean");
            } catch (NamingException ne) {
                throw new RuntimeException(ne);
            }
        }
        return user;
    }

    public static JobRemote getJobBean() {
        if (job == null) {
            try {
                Context c = new InitialContext();
                job = (JobRemote) c.lookup("java:comp/env/JobBean");
            } catch (NamingException ne) {
                throw new RuntimeException(ne);
            }
        }
        return job;
    }

    public static TimeRemote getTimeBean() {
        if (time == null) {
            try {
                Context c = new InitialContext();
                time = (TimeRemote) c.lookup("java:comp/env/TimeBean");
            } catch (NamingException ne) {
                throw new RuntimeException(ne);
            }
        }
        return time;
    }

}
